package com.yuan.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yuan.blog.aop.annotation.Pv;
import com.yuan.blog.aop.type.PvType;
import com.yuan.blog.exception.ViewException;
import com.yuan.blog.service.LikeService;
import com.yuan.blog.utils.R;
import com.yuan.blog.entity.InvitataionEntity;
import com.yuan.blog.entity.UserEntity;
import com.yuan.blog.exception.JsonException;
import com.yuan.blog.service.ICommentService;
import com.yuan.blog.service.IInvitataionService;
import com.yuan.blog.service.IUserService;
import com.yuan.blog.vo.CommentVo;
import com.yuan.blog.vo.InvitataionDataVo;
import com.yuan.blog.vo.InvitataionStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: com.yuan.blog
 * @description:
 * @author: yuan_boss
 * @create: 2022-12-05 21:03
 **/
@Controller
@RequestMapping("/InvitationController")
public class InvitationController{

    @Autowired
    private IInvitataionService invitationService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private LikeService likeService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "web test:" + port;
    }
    @PostMapping("/page")
    @ResponseBody
    public Page<InvitataionEntity> page(@RequestBody Page<InvitataionEntity> page){
        invitationService.page(page);
        return page;
    }

    @PostMapping("/updateType")
    @ResponseBody
    public Boolean updateType(@RequestBody InvitataionStatusVo vo){
        return invitationService.updateType(vo);
    }

    @GetMapping("/invitationInfo/{id}")
    @ResponseBody
    public InvitataionEntity invitationInfo(@PathVariable Integer id) {
        return invitationService.getById(id);
    }

    @PostMapping("/dataList")
    @ResponseBody
    public Map dataList(@RequestBody Page<InvitataionEntity> page) {
        return invitationService.dataList(page);
    }


    @GetMapping("/InvitationInfo/{id}")
    @ResponseBody
    public InvitataionEntity getInvitationInfo(@PathVariable("id") Integer id){
        InvitataionEntity invitataion = invitationService.getById(id);
        return invitataion;
    }



    @PostMapping("/publish")
    @ResponseBody
    public R publish(@RequestBody InvitataionEntity invitataionEntity, HttpServletRequest req) throws Exception {

        // 1.获取登录用户
        UserEntity loginUser = (UserEntity) req.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            throw new JsonException(100,"登录超时,请先登录");
//            return R.error("登录超时，请先登录");
        }

        // 2.封装用户
        invitataionEntity.setUserId(loginUser.getId());
        invitataionEntity.setCreateTime(new Date());
        invitataionEntity.setStatus(1);
        boolean add = invitationService.save(invitataionEntity);
        return R.out(add);
    }

    @GetMapping("/info/{id}")
    @Pv(PvType.INVITATION)
    public String info(@PathVariable("id") Integer id, ModelMap map, HttpSession session) throws Exception {
        // 1.查询帖子
        InvitataionEntity invitataionEntity = invitationService.getById(id);
        // 2.查询作者
        Integer userId = invitataionEntity.getUserId();
        UserEntity userEntity = userService.queryById(userId);
        //获取当前登录用户
        UserEntity loginUser = (UserEntity) session.getAttribute("loginUser");

        // 3.查询帖子的评论
        List<CommentVo> commentEntityList = commentService.commentListByInvitataionId(loginUser,invitataionEntity.getId());

        // 查看帖子的点赞数量
        Long count = likeService.getInvitataionLikeCount(1, id);

        Boolean likeStatus = false;
        if (loginUser != null) {
            // 查看登录用户是否对当前帖子点赞了
            likeStatus = likeService.getInvitataionUserLike(1, loginUser.getId(), id);
        }
        map.put("topic", invitataionEntity);
        map.put("user", userEntity);
        map.put("commentVoList", commentEntityList);
        map.put("likeCount",count);//帖子点赞数量
        map.put("likeStatus",likeStatus ? 1 : 0);// 当前用户是否对这个帖子已经点过赞了


        // 5.跳转到视图
        return "view/site/discuss-detail";
    }
}
